package runtime;

import java.util.Vector;

import symbols.ISymbolTable;

public class ControlStack  {
	private Vector calls = new Vector();

	public ActivationRecord peek() {
		return (ActivationRecord)calls.lastElement();
	}
	
	public ActivationRecord pop() {
		return (ActivationRecord)calls.remove(calls.size()-1);
	}
	
	public void push(ActivationRecord sf) {
		calls.add(sf);
	}
	
	public ISymbolTable getSymbolTable(ISymbolTable st) {
		String name = st.getName();
		int depth = st.getDepth();
		ISymbolTable match = null;
		
		// trying to get the last symbol table with that name and level
		for(int i = calls.size()-1; i >=0; i--) {
			ISymbolTable s = (ISymbolTable)calls.get(i);
			if(s.getName().equals(name) && depth == s.getDepth())
				match = s;
		}
		return match;
	}
}
